package com.itheima.web.listener;

import com.itheima.autoconfig.HuaWeiUGC.HuaWeiUGCTemplate;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.MovementService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RocketMQMessageListener(consumerGroup = "movement-check-group" ,topic = "tanhua-movement-check")
public class MovementCheckListener implements RocketMQListener<String> {

    @DubboReference
    private MovementService movementService;

    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    @Override
    public void onMessage(String message) {
        System.out.println("接受到了消息，需要审核的动态id是"+message);

        //查询该动态
        Movement movement = movementService.findById(message);

        //List<String> medias = movement.getMedias();

        //String[] array = new String[movement.getMedias().size()];
        //String[] strings = movement.getMedias().toArray(array);

        //审核图片，集合转数组
        boolean imageCheck = huaWeiUGCTemplate.imageContentCheck(movement.getMedias().toArray(new String[movement.getMedias().size()]));

        //审核文字
        boolean textCheck = huaWeiUGCTemplate.textContentCheck(movement.getTextContent());

        //判断是否通过审核
        if (imageCheck && textCheck){
            movement.setState(1);
        }else {
            movement.setState(2);
        }

        //更新到mongo中
        movementService.updateMovementById(movement);

        System.out.println("审核结束");
    }
}
